<?php

namespace app\admin\controller\mall;

use app\admin\model\MallOrder;
use app\admin\model\MallUser;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\MallWithdrawRecord;
use app\common\controller\AdminController;
use jianyan\excel\Excel;
use think\App;

/**
 * @ControllerAnnotation(title="余额管理")
 */
class WithdrawRecord extends AdminController
{
    
    
    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\MallWithdrawRecord();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[]             = ['is_virtual', '=', 0];
            $count               = MallUser::where($where)
                ->count();
            $list                = MallUser::where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()
                ->toArray();
            $userIds             = array_column($list, 'id');
            $withdrawBalanceDone = MallWithdrawRecord::where([['user_id', 'in', $userIds], ['status', '=', 2]])->field('SUM(amount) as withdraw_amount_done,user_id')->group('user_id')->select()->toArray();
            $withdrawBalanceDone = array_column($withdrawBalanceDone, null, 'user_id');
            $withdrawBalanceIn   = MallWithdrawRecord::where([['user_id', 'in', $userIds], ['status', 'in', '0,1']])->field('SUM(amount) as withdraw_amount_in,user_id')->group('user_id')->select()->toArray();
            $withdrawBalanceIn   = array_column($withdrawBalanceIn, null, 'user_id');
            $failureSubsidy      = MallOrder::where([['user_id', 'in', $userIds], ['order_status', '=', 40]])->field('SUM(failure_subsidy) as failure_subsidy_amount,user_id')->group('user_id')->select()->toArray();
            $failureSubsidy      = array_column($failureSubsidy, null, 'user_id');
            foreach ($list as $key => $value) {
                $list[ $key ][ 'withdraw_amount_done' ]   = $withdrawBalanceDone[ $value[ 'id' ] ][ 'withdraw_amount_done' ] ?? 0;
                $list[ $key ][ 'withdraw_amount_in' ]     = $withdrawBalanceIn[ $value[ 'id' ] ][ 'withdraw_amount_in' ] ?? 0;
                $list[ $key ][ 'failure_subsidy_amount' ] = $failureSubsidy[ $value[ 'id' ] ][ 'failure_subsidy_amount' ] ?? 0;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    
    /**
     * @NodeAnotation(title="明细")
     */
    public function withdraw()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[] = ['user_id', '=', input('id')];
            $count   = MallWithdrawRecord::where($where)
                ->count();
            $list    = MallWithdrawRecord::where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()
                ->toArray();
            $data    = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('id', input('id'));
        return $this->fetch();
    }
//    /**
//     * @NodeAnotation(title="导出")
//     */
//    public function export()
//    {
//        [$page, $limit, $where] = $this->buildTableParames();
//        $where[]             = ['is_virtual', '=', 0];
//        $header              = [
//            ['用户id', 'id'],
//            ['姓名', 'name'],
//            ['电话号码', 'mobile'],
//            ['头像', 'avatar_url'],
//            ['余额', 'balance'],
//            ['已提现', 'withdraw_amount_done'],
//            ['提现审核中', 'withdraw_amount_in'],
//            ['补贴金额', 'failure_subsidy_amount'],
//        ];
//        $list                = MallUser::where($where)
//            ->limit(100000)
//            ->order($this->sort)
//            ->select()
//            ->toArray();
//        $userIds             = array_column($list, 'id');
//        $withdrawBalanceDone = MallWithdrawRecord::where([['user_id', 'in', $userIds], ['status', '=', 2]])->field('SUM(amount) as withdraw_amount_done,user_id')->group('user_id')->select()->toArray();
//        $withdrawBalanceDone = array_column($withdrawBalanceDone, null, 'user_id');
//        $withdrawBalanceIn   = MallWithdrawRecord::where([['user_id', 'in', $userIds], ['status', 'in', '0,1']])->field('SUM(amount) as withdraw_amount_in,user_id')->group('user_id')->select()->toArray();
//        $withdrawBalanceIn   = array_column($withdrawBalanceIn, null, 'user_id');
//        $failureSubsidy      = MallOrder::where([['user_id', 'in', $userIds], ['order_status', '=', 40]])->field('SUM(failure_subsidy) as failure_subsidy_amount,user_id')->group('user_id')->select()->toArray();
//        $failureSubsidy      = array_column($failureSubsidy, null, 'user_id');
//        foreach ($list as $key => $value) {
//            $list[ $key ][ 'withdraw_amount_done' ]   = $withdrawBalanceDone[ $value[ 'id' ] ][ 'withdraw_amount_done' ] ?? 0;
//            $list[ $key ][ 'withdraw_amount_in' ]     = $withdrawBalanceIn[ $value[ 'id' ] ][ 'withdraw_amount_in' ] ?? 0;
//            $list[ $key ][ 'failure_subsidy_amount' ] = $failureSubsidy[ $value[ 'id' ] ][ 'failure_subsidy_amount' ] ?? 0;
//        }
//        $fileName = '用户列表' . time();
//        return Excel::exportData($list, $header, $fileName, 'xlsx');
//    }
}